/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.entity.dependenceTemp.DataValidator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.nestedConfigs.NestedObjectConfigGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import java.util.ArrayList;
import lombok.var;

public class UdtDataPropertyGenerator extends BaseDataPropertyGenerator {

  private UnifiedDataTypeDef udt;

  protected UnifiedDataTypeDef getUdt() {
    return udt;
  }

  public UdtDataPropertyGenerator(
          IGspCommonField field, UnifiedDataTypeDef udt, DataTypeDataGeneratorContext dataGenContext) {
    super(field, dataGenContext);
    this.udt = udt;
  }

  @Override
  protected TypeInfo getPropertyType() {
    CefGeneratorContext cefContext =
        dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext();
    return Utils.getUdtType(field.getUdtPkgName(), field.getUdtID(), cefContext);
  }

  @Override
  protected String getFieldName() {
    return null;
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    var list =super.getGetterAttributeList();
    if (list == null) {
      list = new ArrayList<AnnotationInfo>();
    }

    if (field.getMappingRelation() == null) {
      throw new RuntimeException();
    }
//    addSimpleUdtAtrributeInfo(list);
    return list;
  }

//  private void addSimpleUdtAtrributeInfo(ArrayList<AnnotationInfo> list) {
//    MediateType configType = dataGenContext.getNestedConfigTypes().get(field.getLabelID());
//    DataValidator.checkForNullReference(configType, "configType");
//    AnnotationInfo info = new AnnotationInfo();
//    info.setAnnotationType(AnnotationType.Normal);
//    var typeInfo = new TypeInfo("NestedConfigAtrribute");
//    typeInfo.setTypePackageName("com.inspur.edp.cef.api.attr");
//    info.setTypeInfo(typeInfo);
//    var param = ast.newTypeLiteral();
//    param.setType(ast.newSimpleType(ast.newName(configType.getClassName())));
//    info.getParams().put("configType",param);
//
//    //TODO 加入import configType.getNamespace()
//    list.add(info);
//  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {

    var list =super.createChildGenerators();
    if(list == null)
      list = new ArrayList<BaseGenerator>();
//    list.add(
//        new NestedObjectConfigGenerator(
//            dataGenContext.getDataTypeGeneratorContext().getDataType(), field, dataGenContext));
    return list;
  }

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    propInfo.setUdt(getUdt());
  }
}
